package com.zzjx.common.tool;

/**
 * web 统一返回json格式数据
 *
 * @param <T>
 */
public class R<T> {


    private int code;

    private String msg;

    private T data;


    private R(Code responseCode) {
        this.code = responseCode.getCode();
        this.msg = responseCode.getDesc();
    }

    private R(Code responseCode, T data) {
        this.code = responseCode.getCode();
        this.msg = responseCode.getDesc();
        this.data = data;
    }

    private R(Code responseCode, String msg) {
        this.code = responseCode.getCode();
        this.msg = msg;
    }

    private R(Code responseCode, String msg, T data) {
        this.code = responseCode.getCode();
        this.msg = msg;
        this.data = data;
    }

    public R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }



    public R(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R() {
        this.code = code;
        this.msg = msg;
    }

    public static R success() {
        return new R(Code.SUCCESS);
    }

    public static R error() {
        return new R(Code.ERROR);
    }

    public static R error(String msg) {
        return new R(Code.ERROR,msg);
    }

    public static R build(Code responseCode) {
        return new R(responseCode);
    }
    public static <T> R<T> data(T data) {
        return new R(Code.SUCCESS,data);
    }

    public static <T> R<T> build(Code responseCode, T data) {
        return new R(responseCode, data);
    }

    public static R build(Code responseCode, String msg) {
        return new R(responseCode, msg);
    }

    public static <T> R<T> build(Code responseCode, String msg, T data) {
        return new R(responseCode, msg, data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    public enum Code {

        SUCCESS(200, "操作成功"),

        ERROR(500, "操作失败");


        private int code;
        private String desc;

        Code(int code, String desc) {
            this.code = code;
            this.desc = desc;
        }

        public int getCode() {
            return code;
        }

        public String getDesc() {
            return desc;
        }

    }


}



